home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / mailconf / generate.c < prev    next >
C/C++ Source or Header  |  1996-07-21  |  7KB  |  245 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/stat.h>
  4. #include "../misc/misc.h"
  5. #include "internal.h"
  6. #include "../paths.h"
  7. #include "../netconf/netconf.h"
  8. #include "mailconf.m"
  9.  
  10. extern MAILCONF_HELP_FILE help_mailconf;
  11.  
  12. CONFIG_FILE f_sendmail (ETC_SENDMAIL_CF,help_mailconf
  13.     ,CONFIGF_GENERATED|CONFIGF_OPTIONNAL|CONFIGF_PROBED);
  14.  
  15.  
  16. /*
  17.     Avoid repeating the same path over and over
  18. */
  19. class MAILCONF_FILE: public CONFIG_FILE{
  20. public:
  21.     MAILCONF_FILE(const char *fname);
  22. };
  23.  
  24. static const char *setpath(const char *fname)
  25. {
  26.     char buf[200];
  27.     sprintf (buf,"%s/mailconf/%s.cf",USR_LIB_LINUXCONF,fname);
  28.     return strdup(buf);
  29. }
  30.  
  31. PUBLIC MAILCONF_FILE::MAILCONF_FILE(const char *fname)
  32.     : CONFIG_FILE (setpath(fname),help_mailconf,CONFIGF_NONE)
  33. {
  34. }
  35.  
  36. static MAILCONF_FILE f_intro ("intro");
  37. static MAILCONF_FILE f_copyright ("copyright");
  38. static MAILCONF_FILE f_stdmacros ("stdmacros");
  39. static MAILCONF_FILE f_stdoptions ("stdoptions");
  40.  
  41. static MAILCONF_FILE f_rulesets ("rulesets");
  42. static MAILCONF_FILE f_rulesets_s0_intro ("rulesets.s0.intro");
  43. static MAILCONF_FILE f_rulesets_s0_local ("rulesets.s0.local");
  44. static MAILCONF_FILE f_rulesets_s0_remote ("rulesets.s0.remote");
  45. static MAILCONF_FILE f_rulesets_s3 ("rulesets.s3");
  46. static MAILCONF_FILE f_rulesets_s96_dns ("rulesets.s96.dns");
  47. static MAILCONF_FILE f_rulesets_s96_nodns ("rulesets.s96.nodns");
  48. static MAILCONF_FILE f_rulesets_s96_fewdns ("rulesets.s96.fewdns");
  49.  
  50. static MAILCONF_FILE f_localmailer ("localmailer.std");
  51. static MAILCONF_FILE f_delivermailer ("localmailer.deliver");
  52. static MAILCONF_FILE f_procmailmailer ("localmailer.procmail");
  53. static MAILCONF_FILE f_progmailer ("progmailer");
  54. static MAILCONF_FILE f_uucpmailer ("uucpmailer.std");
  55. //static MAILCONF_FILE f_uucpnbmailer ("uucpmailer.nobatch");
  56. static MAILCONF_FILE f_smtpmailer ("smtpmailer.std");
  57.  
  58. static MAILCONF_FILE f_localmailer_tst ("localmailer.tst");
  59. static MAILCONF_FILE f_uucpmailer_tst ("uucpmailer.tst");
  60. static MAILCONF_FILE f_smtpmailer_tst ("smtpmailer.tst");
  61.  
  62. const int TOKEN_VALLEN = 40;
  63. /*
  64.     Copy a text file into fout with token replacement
  65.     Return 0 if ok, -1 if any error.
  66. */
  67. static int copyfile (
  68.     CONFIG_FILE &filesrc,
  69.     FILE *fout,
  70.     const char *token[],
  71.     const char repl[][TOKEN_VALLEN],
  72.     int nbtoken)
  73. {
  74.     int ret = -1;
  75.     FILE *fin = filesrc.fopen("r");
  76.     if (fin != NULL){
  77.         ret = 0;
  78.         char buf[1000];
  79.         while (fgets(buf,sizeof(buf)-1,fin)!=NULL){
  80.             for (int i=0; i<nbtoken; i++){
  81.                 const char *tok = token[i];
  82.                 char *pt = strstr(buf,tok);
  83.                 if (pt != NULL){
  84.                     char buf2[1000];
  85.                     int len = (int)(pt-buf);
  86.                     memcpy (buf2,buf,len);
  87.                     strcpy (buf2+len,repl[i]);
  88.                     strcat (buf2,buf+len+strlen(tok));
  89.                     strcpy (buf,buf2);
  90.                 }
  91.             }
  92.             fputs (buf,fout);
  93.         }
  94.         fclose (fin);
  95.     }
  96.     return ret;
  97. }
  98.  
  99. /*
  100.     Copy a text file into fout.
  101.     Return 0 if ok, -1 if any error.
  102. */
  103. static int copyfile (CONFIG_FILE &filesrc, FILE *fout)
  104. {
  105.     return copyfile (filesrc,fout,NULL,NULL,0);
  106. }
  107.  
  108. /*
  109.     Produce the file /etc/sendmail.cf using the configuration.
  110.  
  111.     It mail generate the normal sendmail.cf or a temporary one
  112.     for automated test purpose (If its argument is true).
  113.     Not implemented yet.
  114.  
  115.     Return -1 if any error
  116. */
  117. PUBLIC int MAILCONF::generate(bool)
  118. {
  119.     int ret = -1;
  120.     FILE *fout = f_sendmail.fopen ("w");
  121.     if (fout != NULL){
  122.         ret = copyfile (f_intro,fout);
  123.         ret |= copyfile (f_copyright,fout);
  124.         THISHOST thishost;
  125.         const char *hname = thishost.getname1();
  126.         fputs ("# Alias for this host\n",fout);
  127.         fprintf (fout,"Cw localhost %s",hname);
  128.         int i;
  129.         for (i=0; i<alias.getnb(); i++){
  130.             fprintf (fout," %s",alias.getitem(i)->get());
  131.         }
  132.         fputc ('\n',fout);
  133.  
  134.         fputs ("# who I masquerade as (null for no masquerading)\n",fout);
  135.         const char *masquerade = hname;
  136.         if (!mailas.is_empty()) masquerade = mailas.get();
  137.         fprintf (fout,"DM%s\n",masquerade);
  138.  
  139.         /* #Specification: mailconf / sendmail.cf / smarthost
  140.             If we specify that we are our own smarthost, this
  141.             is causing problem with sendmail. It could be
  142.             argue that it makes sens (independant of sendmail).
  143.  
  144.             Given also that linuxconf mail configuration
  145.             will be shareable one day, this makes sens.
  146.  
  147.             When we detect that the smarthost is this host
  148.             we simply ignore it (The S macro of sendmail.cf
  149.             remain empty).
  150.  
  151.             We do the same for the mailhost (Macro H of
  152.             sendmail.cf)
  153.         */
  154.         fputs ("# Smart host\n",fout);
  155.         if (smarthost.icmp(hname)==0){
  156.             fputs  ("DS\n",fout);
  157.         }else{
  158.             fprintf  (fout,"DS%s\n",smarthost.get());
  159.         }
  160.         fputs ("# Use this mailer to reach the Smart host\n",fout);
  161.         fprintf  (fout,"DN%s\n",smartmailer.get());
  162.  
  163.         fputs ("# Central host for local mail\n",fout);
  164.         if (mailhost.icmp(hname)==0){
  165.             fputs  ("DH\n",fout);
  166.         }else{
  167.             fprintf (fout,"DH%s\n",mailhost.get());
  168.         }
  169.  
  170.         fputs ("# class L: names that should be delivered locally, even if we have a relay\n",fout);
  171.         fprintf (fout,"CL%s\n",users.deliverlocal.get());
  172.  
  173.         fputs ("# class E: names that should be exposed as from this host, even if we masquerade\n",fout);
  174.         fprintf (fout,"CE%s\n",users.dontmasque.get());
  175.  
  176.         fputs ("# Database for special routing\n",fout);
  177.         fprintf (fout,"Kmailertable %s /var/lib/mailertable\n"
  178.             ,features.dbformat.get());
  179.  
  180.         fputs ("# Restrict DNS to those domain only\n",fout);
  181.         fputs ("CD ",fout);
  182.         int restricted_dns = 0;
  183.         for (i=0; i<dnsfor.getnb(); i++){
  184.             SSTRING *item = dnsfor.getitem(i);
  185.             if (!item->is_empty()) restricted_dns = 1;
  186.             fprintf (fout," %s",item->get());
  187.         }
  188.         fputc ('\n',fout);
  189.  
  190.         ret |= copyfile (f_stdmacros,fout);
  191.  
  192.         fputs ("# Deliver mail only in DNS is available\n",fout);
  193.         fputs (features.dnsneeded ? "OI\n" : "#OI\n", fout);
  194.  
  195.         ret |= copyfile (f_stdoptions,fout);
  196.         ret |= copyfile (f_rulesets,fout);
  197.         ret |= copyfile (f_rulesets_s0_intro,fout);
  198.         COMPLEX_ROUTES cplx;
  199.         cplx.rule0 (fout);
  200.         ret |= copyfile (f_rulesets_s0_local,fout);
  201.         ret |= copyfile (f_rulesets_s0_remote,fout);
  202.         ret |= copyfile (f_rulesets_s3,fout);
  203.         if (features.nodns){
  204.             ret |= copyfile (f_rulesets_s96_nodns,fout);
  205.         }else if (restricted_dns){
  206.             ret |= copyfile (f_rulesets_s96_fewdns,fout);
  207.         }else{
  208.             ret |= copyfile (f_rulesets_s96_dns,fout);
  209.         }
  210.         if (deliver.is_empty()){
  211.             if (file_exist ("/bin/mail.local")){
  212.                 ret |= copyfile (f_localmailer,fout);
  213.             }else{
  214.                 ret |= copyfile (f_delivermailer,fout);
  215.             }
  216.         }else if (deliver.cmp("deliver")==0){
  217.             ret |= copyfile (f_delivermailer,fout);
  218.         }else if (deliver.cmp("procmail")==0){
  219.             ret |= copyfile (f_procmailmailer,fout);
  220.         }
  221.         ret |= copyfile (f_progmailer,fout);
  222.         static const char *uucptoken[]={
  223.             "$(MAXSIZ)","$(UUXOPT)"
  224.         };
  225.         char uucprepl[2][TOKEN_VALLEN];
  226.         uucprepl[0][0] = '\0';
  227.         if (features.uucpmax > 0){
  228.             sprintf (uucprepl[0],"M=%d,",features.uucpmax);
  229.         }
  230.         strcpy (uucprepl[1],features.uucpnobatch ? "" : "-r");
  231.         ret |= copyfile (f_uucpmailer,fout
  232.             ,uucptoken,uucprepl,2);
  233.         ret |= copyfile (f_smtpmailer,fout);
  234.         fclose (fout);
  235.         spcs.build(features.dbformat.get());
  236.         if (ret != -1){
  237.             xconf_notice (MSG_U(N_HASGEN,"%s has been regenerated!")
  238.                 ,f_sendmail.getpath());
  239.         }
  240.     }
  241.     return ret;
  242. }
  243.     
  244.  
  245.